import { WeaLocaleProvider } from 'ecCom';

class DocMouldWord extends React.Component {
  constructor(props) {
    super(props);
    this.init = false;
  }

  componentWillUnmount() {
    if (this.refs.iframe) {
      this.refs.iframe.contentWindow.UnLoad();
    }
  }

  render() {
    const { id = "", docType = ".doc", userName, isIWebOffice2006, showOffice, loadOffice, webUrl } = this.props;
    let src = webUrl + "?" +
      "mRecordID=" + id +
      "&mFileType=" + docType +
      "&mUserName=" + userName +
      "&mEditType=" + (isIWebOffice2006 ? "1,1" : "1");
    let language = WeaLocaleProvider.getUserLanguage();
    if (language == 7) {
      src += "&Language=CH";
    } else if (language == 9) {
      src += "&Language=TW";
    } else {
      src += "&Language=EN";
    }
    // 做一个只需加载一次的处理
    if (!loadOffice && !this.init) {
      return (<div style={{ display: 'none' }} />)
    }
    this.init = true;
    return (
      <div style={{ height: 600, display: showOffice && loadOffice ? 'block' : 'none' }}>
        <iframe ref="iframe" src={src} style={{ display: "block", width: "100%", height: "100%", border: 0 }}></iframe>
      </div>
    );
  }

  saveOffice = () => {
    return this.refs.iframe.contentWindow.SaveDocument();
  }

  saveBookmarks = ({ isEditMould = false }) => {
    let webOfficeIframe = this.refs.iframe.contentWindow;
    let webOffice = webOfficeIframe.document.getElementById("WebOffice");
    const webObject = webOffice.WebObject, bookmarks = webObject.Bookmarks;
    if (bookmarks != '' || bookmarks != null) {
      let bookmarkNames = "";
      for (let i = 1; i <= bookmarks.Count; i++) {
        if (bookmarkNames) {
          bookmarkNames += ","
        }
        bookmarkNames += bookmarks.Item(i).Name;
      };
      webOffice.WebSetMsgByName("BOOKMARKNAMES", bookmarkNames);
    }
    webOffice.WebSetMsgByName("ISEDITMOULD", isEditMould.toString().toUpperCase());
    if (!webOffice.WebSaveBookmarks()) {
      return false;
    } else {
      return true;
    }
  }
}

export default DocMouldWord;